基础知识

环境

初始环境

docker启动

1
2
docker run -d -p 8088:80 --name nginx_8088 nginx_80:latest /sbin/init
复制代码

四项确认

  • 确认系统网络(ping)
  • 确认yum可用 (yum list | grep gcc
  • 确认关闭iptables (iptables -F)
  • 确认停用selinux

两项安装

1
2
3
yum -y install gcc gcc-c++ autoconf pcre pcre-devel make automake
yum -y install wget httpd-tools vim
复制代码

一次初始化

1
2
cd /opt/ mkdir app download logs work backup
复制代码

nginx安装

确定nginx源

1
2
3
4
5
6
7
8
9
10
11
cd /etc/yum.repos.d
vim nginx.repo

添加:

name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

复制代码

安装

1
2
3
4
yum list | grep nginx
yum install nginx

复制代码

查看版本

1
2
nginx -v
复制代码

查看nginx 编译的参数

1
2
nginx -V
复制代码

nginx启动

1
2
nginx -c /etc/nginx/nginx.conf 
复制代码

重启nginx服务

1
2
systemctl restart nginx.service
复制代码

柔和重启

1
2
nginx -s reload -c /etc/nginx/nginx.conf
复制代码

检查配置文件

1
2
nginx -t -c /etc/nginx/nginx.conf
复制代码

中间件架构

image-20190802143449669

image-20190802143550357

image-20190802143607065

image-20190802143632910


image-20190802143711349


nginx简述

nginx是一个开源且高性能、可靠的http中间件,代理服务。Nginx(发音同engine x)是一个 Web服务器,也可以用作反向代理,负载平衡器和 HTTP缓存。该软件由 Igor Sysoev 创建,并于2004年首次公开发布。同名公司成立于2011年,以提供支持。

为什么选择nginx

io多路复用epoll

多个描述符的i/o操作都能在一个线程内并发交替地顺序完成,这就教i/o多路复用,这里的”复用“指的是复用同一个线程。i/o多路复用的实现方式为:select、poll、epoll

image-20190802143807662

什么是select

image-20190802143833792


epoll模型

  • 当FD就绪,采用系统的回调函数之间将fd放入,效率更高
  • 最大连接无限制

轻量级

  • 功能模块少
  • 代码模块化

cpu亲和(affinity)

是一种cpu核心和nginx工作进程绑定方式,把每个worker进程固定在一个cpu上执行,减少切换cpu的cache miss,获得更好的性能。

image-20190802143858940

说白了就是减少cpu切换所损耗的性能

sendfile

image-20190802143918019



nginx版本

  • Mainline version 开发版
  • Stable version 稳定版
  • Legacy version 历史版本

基本参数使用

rpm

rpm命令是RPM软件包的管理工具。rpm原本是Red Hat Linux发行版专门用来管理Linux各项套件的程序,由于它遵循GPL规则且功能强大方便,因而广受欢迎。逐渐受到其他发行版的采用。RPM套件管理方式的出现,让Linux易于安装,升级,间接提升了Linux的适用度。

安装目录

列出服务的安装目录

1
rpm -ql nginx

列出下面目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/etc/logrotate.d/nginx
/etc/nginx
/etc/nginx/conf.d
/etc/nginx/conf.d/default.conf
/etc/nginx/fastcgi_params
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/modules
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
/etc/nginx/win-utf
/etc/sysconfig/nginx
/etc/sysconfig/nginx-debug
/usr/lib/systemd/system/nginx-debug.service
/usr/lib/systemd/system/nginx.service
/usr/lib64/nginx
/usr/lib64/nginx/modules
/usr/libexec/initscripts/legacy-actions/nginx
/usr/libexec/initscripts/legacy-actions/nginx/check-reload
/usr/libexec/initscripts/legacy-actions/nginx/upgrade
/usr/sbin/nginx
/usr/sbin/nginx-debug
/usr/share/doc/nginx-1.12.2
/usr/share/doc/nginx-1.12.2/COPYRIGHT
/usr/share/man/man8/nginx.8.gz
/usr/share/nginx
/usr/share/nginx/html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
/var/cache/nginx
/var/log/nginx
复制代码

目录解释

路径 类型 作用
/etc/logrotate.d/nginx 配置文件 nginx日志轮转,用于logrotate服务的日志切割
/etc/nginx /etc/nginx/conf.d /etc/nginx/conf.d/default.conf /etc/nginx/nginx.conf 目录、配置文件 nginx主配置文件
/etc/nginx/fastcgi_params /etc/nginx/scgi_params /etc/nginx/uwsgi_params 配置文件 cgi配置相关,fastcgi配置
/etc/nginx/koi-utf /etc/nginx/koi-win /etc/nginx/win-utf 配置文件 编码映射转化文件
/etc/nginx/mime.types 配置文件 设置http协议的Content-Type与扩展名对应关系
/etc/sysconfig/nginx /etc/sysconfig/nginx-debug /usr/lib/systemd/system/nginx-debug.service /usr/lib/systemd/system/nginx.service 配置文件 用于配置出系统守护进程管理器管理方式
/etc/nginx/modules /usr/lib64/nginx/modules 目录 nginx模块目录
/usr/sbin/nginx /usr/sbin/nginx-debug 命令 nginx服务的启动管理的终端命令
/usr/share/doc/nginx-1.12.2 /usr/share/doc/nginx-1.12.2/COPYRIGHT /usr/share/man/man8/nginx.8.gz 文件目录 nginx的手册和帮助文件
/var/cache/nginx 目录 nginx的缓存目录
/var/log/nginx 目录 nginx的日志目录

编译参数

列出编译参数的命令

1
nginx -V

结果

1
2
3
4
5
6
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
复制代码

** 参数解释**

路径 类型
–prefix=/etc/nginx –sbin-path=/usr/sbin/nginx –modules-path=/usr/lib64/nginx/modules –conf-path=/etc/nginx/nginx.conf –error-log-path=/var/log/nginx/error.log –http-log-path=/var/log/nginx/access.log –pid-path=/var/run/nginx.pid –lock-path=/var/run/nginx.lock 安装目的目录或路径
–http-client-body-temp-path=/var/cache/nginx/client_temp –http-proxy-temp-path=/var/cache/nginx/proxy_temp –http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp –http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp –http-scgi-temp-path=/var/cache/nginx/scgi_temp 执行对应模块时,nginx所保留的临时性文件
–user=nginx –group=nginx 设定nginx进程启动的用户和组用户
–with-cc-opt=parameters 设置额外的参数将被添加到CFLAGS变量
–with-ld-opt=parameters 设置附加的参数,链接系统库

nginx基本配置语法

http相关

展示每次请求的请求头

1
2
curl -v http://www.baidu.com
复制代码

结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* About to connect() to www.baidu.com port 80 (#0)
* Trying 61.135.169.121...
* Connected to www.baidu.com (61.135.169.121) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: www.baidu.com
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: bfe/1.0.8.18
< Date: Thu, 30 Nov 2017 02:14:02 GMT
< Content-Type: text/html
< Content-Length: 2381
< Last-Modified: Mon, 23 Jan 2017 13:27:32 GMT
< Connection: Keep-Alive
< ETag: "588604c4-94d"
< Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
< Pragma: no-cache
< Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
< Accept-Ranges: bytes
<
{ [data not shown]
100 2381 100 2381 0 0 88266 0 --:--:-- --:--:-- --:--:-- 91576
* Connection #0 to host www.baidu.com left intact

复制代码

nginx日志类型

  • error.log、 access.log
  • log_format

格式*

1
2
3
4
syntax: log_format name [escape=default | json] string...;
default: log_format combined "...";
context:http
复制代码

nginx变量

nginx配置的内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
user  nginx;
worker_processes 1;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;


events {
worker_connections 1024;
}


http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;
#tcp_nopush on;

keepalive_timeout 65;

#gzip on;

include /etc/nginx/conf.d/*.conf;
}

复制代码

变量类型

  • http请求变量:arg_PARAMETER,http_header,sent_http_header
  • 内置变量:nginx内置的
  • 自定义变量: 自己定义

nginx模块

  • nginx官方模块
  • 第三方模块

default.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
server {
listen 80;
server_name localhost;

#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;

location / {
root /usr/share/nginx/html;
index index.html index.htm;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}

复制代码

nginx开启的模块

1
2
 --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module
复制代码

安装编译模块

编译选项 作用
–with-http_stub_status_module nginx的客户端状态
–with-http_random_index_module 目录中选择一个随机主页
–with-http_sub_module http内容替换
–limit_conn_module 连接频率限制
–limit_req_module 请求频率限制
http_access_module 基于ip的访问控制
http_auth_basic_module 基于用户的信任登录

http_stub_status_module 配置

配置语法

1
2
3
4
syntax: stub_status;
default:-
context:server, location
复制代码

在default.conf中添加:

1
2
3
4
5
# my config
location /mystatus {
stub_status;
}
复制代码

检查和重新启动配置

1
2
nginx -tc /etc/nginx/nginx.conf 
复制代码

重启服务

1
2
nginx -s reload -c /etc/nginx/nginx.conf 
复制代码

检查效果输入:http://127.0.0.1:8088/mystatus

image-20190802144325397

表示nginx的活跃连接数;握手的总次数、处理连接数;读、写、等待个数

http_random_index_module

配置语法

1
2
3
4
syntax: random_index on | off;
default:random_index off;
context:location
复制代码

在default.conf中将下面的配置:

1
2
3
4
5
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
复制代码

改为:

1
2
3
4
5
location / {
root /usr/share/nginx/html;
random_index on;
#index index.html index.htm;
}

image-20190806063031069

image-20190806063141639

image-20190806063209886

image-20190806064413482

image-20190806064540194

image-20190806064723921

image-20190806064923176

image-20190806065247407